package com.bdqn.zhang.servlet;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.zhang.pojo.Dept;
import com.bdqn.zhang.pojo.User;
import com.bdqn.zhang.service.DeptService;
import com.bdqn.zhang.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "DeptServlet")
public class DeptServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        DeptService deptService = new DeptServiceImpl();
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        if("open".equals(opr)){
            request.getRequestDispatcher("/WEB-INF/user/user.html").forward(request,response);

        } else if("add".equals(opr)){
            request.getRequestDispatcher("/WEB-INF/user/useradd.html").forward(request,response);
        } else if("addSave".equals(opr)){
            Dept u = new Dept();
            u.setParentId(Integer.parseInt(request.getParameter("parent_id")));
            u.setAncestors(request.getParameter("ancestors"));
            u.setDeptName(request.getParameter("dept_name"));
            u.setOrderNum(Integer.parseInt(request.getParameter("order_num")));
            u.setLeader(request.getParameter("leader"));
            u.setPhone(request.getParameter("phone"));
            u.setEmail(request.getParameter("email"));
            u.setStatus(request.getParameter("status"));
            u.setDelFlag(request.getParameter("del_flag"));
            u.setCreate_by(request.getParameter("create_by"));
            u.setCreate_time(request.getParameter("create_time"));
            u.setUpdate_by(request.getParameter("update_by"));
            u.setUpdate_time(request.getParameter("update_time"));
            int count = deptService.insertDept(u);
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        } else if("edit".equals(opr)){
            Dept dept = deptService.selectDeptById(Integer.parseInt(request.getParameter("id")));
            request.getSession().setAttribute("user",dept);
            request.getRequestDispatcher("/WEB-INF/user/useredit.html").forward(request,response);
        } else if("editShowData".equals(opr)){
            User user = (User)request.getSession().getAttribute("user");
            request.getSession().removeAttribute("user");
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONStringWithDateFormat(user,"yyyy-MM-dd HH:mm:ss"));
        } else if("editSave".equals(opr)){
            Dept u = new Dept();
            u.setParentId(Integer.parseInt(request.getParameter("parent_id")));
            u.setAncestors(request.getParameter("ancestors"));
            u.setDeptName(request.getParameter("dept_name"));
            u.setOrderNum(Integer.parseInt(request.getParameter("order_num")));
            u.setLeader(request.getParameter("leader"));
            u.setPhone(request.getParameter("phone"));
            u.setEmail(request.getParameter("email"));
            u.setStatus(request.getParameter("status"));
            u.setDelFlag(request.getParameter("del_flag"));
            u.setCreate_by(request.getParameter("create_by"));
            u.setCreate_time(request.getParameter("create_time"));
            u.setUpdate_by(request.getParameter("update_by"));
            u.setUpdate_time(request.getParameter("update_time"));
            int count = deptService.updateDept(u);
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        } else if("detail".equals(opr)){
            Dept user = deptService.selectDeptById(Integer.parseInt(request.getParameter("id")));
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("/WEB-INF/user/userdetail.html").forward(request,response);
        } else if("detailShowData".equals(opr)){
            User user = (User)request.getSession().getAttribute("user");
            request.getSession().removeAttribute("user");
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONStringWithDateFormat(user,"yyyy-MM-dd HH:mm:ss"));
        } else if("removeAll".equals(opr)){
            int count = deptService.deleteDeptByIds(request.getParameter("ids"));
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        } else if("del".equals(opr)){
            int count = deptService.deleteDeptById(Integer.parseInt(request.getParameter("id")));
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
